package com.example.shopp_2301

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.viewpager2.widget.ViewPager2
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.example.base.mvvm.BaseViewActivity
import com.example.shopp_2301.adapter.VpAdapter
import com.example.shopp_2301.databinding.ActivityWelBinding
import com.example.shopp_2301.room.AppDbUtils
import com.example.shopp_2301.room.Fen
import com.example.shopp_2301.welfg.BlankFragment1
import com.example.shopp_2301.welfg.BlankFragment2
import com.example.shopp_2301.welfg.BlankFragment3
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.launch

class WelActivity : BaseViewActivity<ActivityWelBinding>() {
    val list = mutableListOf<Fragment>()
    //定义
    lateinit var vpAdapter: VpAdapter
    var dang = 0//方便后续使用
    lateinit var blankFragment1: BlankFragment1
    override fun initView() {

        requestPermissions(arrayOf("android.permission.WRITE_EXTERNAL_STORAGE","android.permission.READ_EXTERNAL_STORAGE"),200)
        //如果处于免登录时间，直接进入主页
        val long = SPUtils.getInstance().getLong("time")
        if(System.currentTimeMillis()-long<60*1000){
            ARouter.getInstance().build("/app/ShopActivity").navigation()
        }
        //添加数据
        blankFragment1 = BlankFragment1()
        list.add(blankFragment1)
        list.add(BlankFragment2())
        list.add(BlankFragment3())
        //实例化适配器
        vpAdapter = VpAdapter(this,list)
        binding.vp.adapter = vpAdapter
        //左右滑动
        binding.vp.registerOnPageChangeCallback(object:ViewPager2.OnPageChangeCallback(){
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                when(position){
                    0->binding.rg.check(R.id.rb1)
                    1->binding.rg.check(R.id.rb2)
                    2->binding.rg.check(R.id.rb3)
                }
            }
        })
        //点击rg
        binding.rg.setOnCheckedChangeListener { group, checkedId ->
            when(checkedId){
                R.id.rb1->binding.vp.currentItem = 0
                R.id.rb2->binding.vp.currentItem = 1
                R.id.rb3->binding.vp.currentItem = 2
            }
        }
        binding.start.setOnClickListener {
            val list = mutableListOf(1,4,3,6,2)
            println(findEvenNumbers(list))
            val person = Person()
            person.age = 20
            person.name = "张三"
            person.email = "zhangsan@qq.com"
            println(person)
            println(person.isAdult())
            println(person.toString())
            method3()
            println(meth { 234455 })
            //构建一个flow，完成发送数据，接收数据
            lifecycleScope.launch {
                val flow = flowOf(1,2)
                flow.collect{
                    println("接收到的数据$it")
                }
            }
            //构建一个整数数组，循环往里面添加10条数据，取出数组中所有的偶数
            var nums = arrayOf<Int>()
            for (i in 1..10){
                nums+=i
            }
            val ints = nums.filter { it % 2 == 0 }
            println(ints)
            //调用
            println("zhangsan".count('a'))
            //调用
            val person1 = Person1("张三",18,"八维")
            updatePersonDetails(person1){
                it.name = "李四"
                it.age = 19
            }
            println(person1)
            //定义集合，存放Emp
            val emps = mutableListOf<Emp>()
            //使用apply在集合中添加20条记录，并打印集合
            list.apply {
                for (i in 1..10){
                    emps.add(Emp("张三",i))
                }
                println(emps)
            }

        }
        total()//统计上方数据
        //点击赚积分
        binding.add.setOnClickListener {
            AppDbUtils.getDb().getFenDao().insert(Fen(0,46,1))
            total()//统计上方数据
            blankFragment1.queryAll()//fragment发生变化
        }
        //点击扣积分
        binding.sub.setOnClickListener {
            if(dang>200){
                AppDbUtils.getDb().getFenDao().insert(Fen(0,200,2))
                total()
                blankFragment1.queryAll()
            }
        }

    }

    private fun total() {
        dang = 0
        var shouru = 0
        var zhichu = 0
        val listShou = AppDbUtils.getDb().getFenDao().queryType(1)
        listShou.forEach {
            shouru+=it.fen
        }
        val listChu = AppDbUtils.getDb().getFenDao().queryType(2)
        listChu.forEach {
            zhichu+=it.fen
        }
        dang = shouru-zhichu
        binding.tv.text = "当前：$dang    收入：$shouru   支出：$zhichu"
    }

    override fun initData() {

    }

    override fun getLayout(): Int {
        return R.layout.activity_wel
    }

}
//参数为整数列表，返回整数列表中所有偶数的平方并升序排序
fun findEvenNumbers(numbers:MutableList<Int>):List<Int>{
    val list1 = numbers.filter { it % 2 == 0 }.map { it * it }.sorted()
    return list1
}
//定义类，添加方法判断是否成年，重写toString 方法
class Person{
    var name:String = ""
    var age:Int = 0
    var email:String = ""

    fun isAdult():String{
        if(age>=18){
            return "成年了"
        }else{
            return "还未成年"
        }
    }

    override fun toString(): String {
        return "Person(name=$name,age=$age,email=$email)"
    }

}
//循环向集合中添加7的倍数，并输出
fun method3(){
    val list = mutableListOf<Int>()
    for (i in 1..4){
        list.add(i*7)
    }
    println(list)
}
//定义meth,参数为(Int)->Unit，调用并返回结果
fun meth(func:(Int)->Unit):String{
    return "123"
}
//写一个String的扩展函数并调用，用于统计某个字符出现的次数
fun String.count(char: Char):Int{
    val str = char
    var count = 0
    this.toCharArray().toList().forEach {
        if(str==it){
            count+=1
        }
    }
    return count
}
//编写一个内联函数inlineSum 它接受一个整数列表和一个整数累加器作为参数，该函数需要遍历列表，并使用累加器对每个元素进行累加操作。最后返回累加后的结果
inline fun inlineSum(list:MutableList<Int>,count:Int):Int{
    var sum = count
    list.forEach {
        sum+=it
    }
    return sum
}
//定义数据类，编写函数 参数为person和lambda表达式，内部使用with更新数据
data class Person1(var name:String,var age:Int,var address:String)
fun updatePersonDetails(person1: Person1,func:(Person1)->Unit){
    with(person1){
        func(this)
    }
}
//定义数据类
data class Emp(var name:String,var age:Int)
